package com.ruoyi.web.controller.tourism;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.entity.Vo.ObjectItem;
import com.ruoyi.system.service.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Y.C
 * @CreateTime: 2022/5/20 16:57
 * @Description: minio控制器
 */
@Api(tags = "文件上传接口")
@RestController
@RequestMapping("/minio")
public class MinioController {
    @Autowired
    private MinioService minioService;

    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.bucketName}")
    private String bucketName;

    /**
     * 批量上传文件
     * @param files
     * @return
     */
    @CrossOrigin
    @ApiOperation("批量上传文件")
    @Anonymous
    @PostMapping("/uploads")
    public List<String> uploads(@RequestPart("file") MultipartFile[] files) {
        List<String> fileUrls = new ArrayList<>();
        List<String> fileNames = minioService.uploads(files);
        for (String fileName : fileNames) {
            fileUrls.add(endpoint + "/" + bucketName + "/" +fileName);
        }
        return fileUrls;
    }

    /**
     * 获取文件对象列表
     * @return
     */
    @ApiOperation("获取文件对象列表")
    @Anonymous
    @GetMapping("/listObjects")
    public List<ObjectItem> listObjects() {
        List<ObjectItem> objectItems = minioService.listObjects(bucketName);
        return objectItems;
    }
}
